// const db = wx.cloud.database().collection("bannerinfo")
const cont = wx.cloud.database({});
const db = cont.collection('bannerinfo');

// const pay = wx.cloud.database().collection("subben_pay")
// pages/zhuye/zhuye.js
Page({
  data: {
    kongge:"\xa0\xa0\xa0\xa0",
    tab:0,
    scrollTop:'0',
    scrollY:'',
    maxlei:"大类",
    maxlei:"小类",
    array1:{
      "0":['三餐', '早餐', '中餐', '晚餐', '买菜', '水果', '零食', '下午茶', '烟酒饮品', '粮酒调料'],
      "1":['公交地铁', '打车', '飞机火车', '共享单车', '加油'],
      "2":['日用品', '衣帽鞋包', '护肤美妆', '首饰品', '数码', '电器', '家装'],
      "3":['水电煤', '话费', '网费', '房费', '物业', '维修'],
      "4":['送礼', '请客', '孝心', '亲密付'],
      "5":['药品', '保健', '治疗', '美妆'],
      "6":['休闲', '约会', '聚会', '游戏', '健身'],
      "7":['书籍', '培训', '网课', '考试'],
      "8":['房贷', '车贷', '购物分期', '手续费', '养卡'],
      "9":['旅游', '装修', '宝宝', '生意', '宠物', '还钱', '借出']
    }
  },
  async onLoad(e) {
    let images = await wx.cloud.callFunction({
      name:'getimages'
    })
    let img = images.result.images.list
    console.log(db)
    console.log("img",img)
    var that = this
      // db.get({
        // success:res=>{
          // console.log("获取图片成功",res)
          let imageheader = img.map(v => {
            v.key = Math.random()
            return v
          }).sort((a,b)=>{
            return a.key - b.key
          }).slice(-9)
          that.setData({
            image:imageheader[0].coverImg,
            title:imageheader[0].title
          // })
        // }
        // ,
        // fail(res){
        //   console.log("获取图片失败",res)
        // }   
      })
      //pay
      let pay = await wx.cloud.callFunction({
        name:'getpay'
      })
      let listpay = pay.result.directions.list
      console.log(listpay)
      // console.log(listpay[].leibie[0])
      this.setData({
        listpay,
      })
      for(let i=0;i<listpay.length;i++){
        let maxlei = listpay[i].leibie[0]
        let minlei = listpay[i].leibie[1]
        // console.log(this.data.array1["1"][minlei])
        var arr1 = listpay.map(v=>{
          // console.log("map",v.leibie[0])
          return v.leibie[0]    //获取所有大类下标
        })
        switch(maxlei){
          case 0:
            listpay[i].maxlei = "餐饮"
            listpay[i].minlei = this.data.array1["0"][minlei]
          break;
          case 1:
            listpay[i].maxlei = "交通"
            listpay[i].minlei = this.data.array1["1"][minlei]
          break;
          case 2:
            listpay[i].maxlei = "购物"
            listpay[i].minlei = this.data.array1["2"][minlei]
          break;
          case 3:
            listpay[i].maxlei = "居家"
            listpay[i].minlei = this.data.array1["3"][minlei]
          break;
          case 4:
            listpay[i].maxlei = "人情"
            listpay[i].minlei = this.data.array1["4"][minlei]
          break;
          case 5:
            listpay[i].maxlei = "医疗"
            listpay[i].minlei = this.data.array1["5"][minlei]
          break;
          case 6:
            listpay[i].maxlei = "娱乐"
            listpay[i].minlei = this.data.array1["6"][minlei]
          break;
          case 7:
            listpay[i].maxlei = "学习"
            listpay[i].minlei = this.data.array1["7"][minlei]
          break;
          case 8:
            listpay[i].maxlei = "金融"
            listpay[i].minlei = this.data.array1["8"][minlei]
          break;
          case 9:
            listpay[i].maxlei = "其他"
            listpay[i].minlei = this.data.array1["9"][minlei]
          break;
        }
        // console.log(listpay)
        this.setData({
          listpay:listpay
        })
      }
      console.log(arr1)


    },
  async onShow() {
    // console.log('首页')
    if(typeof this.getTabBar === 'function' && this.getTabBar()){
      this.getTabBar().setData({
        selected:0
      })
    }

    //pay
    let pay = await wx.cloud.callFunction({
      name:'getpay'
    })
    let listpay = pay.result.directions.list
    console.log(listpay)
    // console.log(listpay[].leibie[0])
    this.setData({
      listpay,
    })
    for(let i=0;i<listpay.length;i++){
      let maxlei = listpay[i].leibie[0]
      let minlei = listpay[i].leibie[1]
      // console.log(this.data.array1["1"][minlei])
      var arr1 = listpay.map(v=>{
        // console.log("map",v.leibie[0])
        return v.leibie[0]    //获取所有大类下标
      })
      switch(maxlei){
        case 0:
          listpay[i].maxlei = "餐饮"
          listpay[i].minlei = this.data.array1["0"][minlei]
        break;
        case 1:
          listpay[i].maxlei = "交通"
          listpay[i].minlei = this.data.array1["1"][minlei]
        break;
        case 2:
          listpay[i].maxlei = "购物"
          listpay[i].minlei = this.data.array1["2"][minlei]
        break;
        case 3:
          listpay[i].maxlei = "居家"
          listpay[i].minlei = this.data.array1["3"][minlei]
        break;
        case 4:
          listpay[i].maxlei = "人情"
          listpay[i].minlei = this.data.array1["4"][minlei]
        break;
        case 5:
          listpay[i].maxlei = "医疗"
          listpay[i].minlei = this.data.array1["5"][minlei]
        break;
        case 6:
          listpay[i].maxlei = "娱乐"
          listpay[i].minlei = this.data.array1["6"][minlei]
        break;
        case 7:
          listpay[i].maxlei = "学习"
          listpay[i].minlei = this.data.array1["7"][minlei]
        break;
        case 8:
          listpay[i].maxlei = "金融"
          listpay[i].minlei = this.data.array1["8"][minlei]
        break;
        case 9:
          listpay[i].maxlei = "其他"
          listpay[i].minlei = this.data.array1["9"][minlei]
        break;
      }
      // console.log(listpay)
      this.setData({
        listpay:listpay
      })
    }

    
    
  },

  //滚动监听
  scroll: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      scrollY: e.detail.scrollTop
    })
    // console.log(this.data.scrollY)
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  gotoday (){
    wx.navigateTo({
      url: './today/index'　　// 页面 A
    })
  },
  async changeItem(e){
    console.log("选项卡切换",e)
    this.setData({
      tab: e.target.dataset.item
    })

    // income
    let income = await wx.cloud.callFunction({
      name:'getincome'
    })
    console.log(income)
    let listincome = income.result.directions.list
    console.log(listincome)
    console.log(listincome[0].leibie)
    this.setData({
      listincome,
    })
    for(let i=0;i<listincome.length;i++){
      let maxlei = listincome[i].leibie[0]
      let minlei = listincome[i].leibie[1]
      console.log(maxlei)
      console.log(minlei)
    }

    for(let i=0;i<listincome.length;i++){
      let maxlei = listincome[i].leibie[0]
      let minlei = listincome[i].leibie[1]
      console.log("income",listincome[i])
      switch(maxlei){
        case 0:
          listincome[i].maxlei1 = "职业工资"
          listincome[i].minlei1 = this.data.array1["0"][minlei]
        break;
        case 1:
          listincome[i].maxlei1 = "其他收入"
          listincome[i].minlei1 = this.data.array1["1"][minlei]
        break;
      }
      this.setData({
        listincome:listincome
      })
    }
  }
})